
************* PROPORTIONALITY DATA *********************

cd "path"
use "Partydata.dta", clear

rename partyseathare partyseatshare

* Modified Gallagher index
generate votessquare=partyvoteshare^2
generate seatssquare=partyseatshare^2

egen sumvotessquare = total(votessquare), by(year munic)
egen sumseatssquare = total(seatssquare), by(year munic)

generate left = partyseatshare/(sumseatssquare)^0.5
generate right = partyvoteshare/(sumvotessquare)^0.5

generate tosum = (left-right)^2
egen summed = total(tosum), by(year munic)
generate MG = (0.5 * summed )^0.5

rename MG modifiedgallagher

* Slope index
gen dif = partyseatshare-partyvoteshare

gen slope=.
egen group = group(munic year)
su group, meanonly
forvalue i = 1/`r(max)' {
reg dif partyvoteshare if group == `i' 
replace slope=_b[partyvoteshare] if group == `i' 
}
sum slope
rename slope differences

drop if pop_el>22500
gen pop_el0=.
replace pop_el0=pop_el-2000 if pop_el>0 & pop_el<=3000
replace pop_el0=pop_el-4000 if pop_el>3000 & pop_el<=6000
replace pop_el0=pop_el-8000 if pop_el>6000 & pop_el<=11500
replace pop_el0=pop_el-15000 if pop_el>11500 & pop_el<=22500 

by year munic, sort: gen tempmun=1 if _n==1 
keep if tempmun==1
keep year munic pop_el pop_el0 modifiedgallagher differences
sum
save "Proportionality_replication_data", replace


************* COHESION DATA *********************

clear all
clear mata
graph drop _all
mata: mata set matastrict off, permanently
program drop _all
macro drop _all
matrix drop _all
set more off
set memory 1G
set type double
set matsize 10000

* Constructing data set

* Start with proprietary individual candidate level election results and charactetistics data
cd "path"
use "incumbency with individual and municipality characteristics 170614.dta", clear

* Drop duplicates
duplicates drop hetu vaalivuosi, force

replace kunta=municipality if vaalivuosi==2012
replace Puolue   = subinstr(Puolue," ","",.)
replace Puolue=puolue if Puolue==""

sort kunta Puolue vaalivuosi

* merge pre-electoral alliances information
merge m:1 kunta Puolue vaalivuosi using "alliances 220216.dta"
drop if _merge==2
drop _merge

drop valtuustokoko
bysort Kuntaid vaalivuosi: egen valtuustokoko=sum(valittu)

* Fixing erroneous alliances by hand

* check e.g. http://tulospalvelu.vaalit.fi/K2008/e/ehd_listat/eura.html

replace alliance="" if kunta=="Leppävirta" & Puolue=="KESK"
replace alliance="" if kunta=="Oulunsalo" & vaalivuosi==1996 & (Puolue=="KOK" | Puolue=="LKP" | Puolue=="SKL" | Puolue=="RKP")
replace alliance="" if kunta=="Nokia" & vaalivuosi==1996 & (Puolue=="RKP" | Puolue=="SKL" | Puolue=="KOK" | Puolue=="LKP")
replace alliance="" if kunta=="Nokia" & vaalivuosi==2000 & (Puolue=="RKP" | Puolue=="KOK")
replace alliance="" if Kuntaid==15 & vaalivuosi==2008 
replace alliance="" if Kuntaid==418 & vaalivuosi==2008 
replace alliance="" if Kuntaid==483 & vaalivuosi==2008 
replace alliance="" if Kuntaid==916 & vaalivuosi==2008 
replace alliance="" if Kuntaid==972 & vaalivuosi==2008 
replace alliance="" if Kuntaid==50 & vaalivuosi==2012 
replace alliance="" if Kuntaid==413 & vaalivuosi==2012 
replace alliance="" if Kuntaid==611 & vaalivuosi==2012 
replace alliance="" if Kuntaid==691 & vaalivuosi==2012 
replace alliance="KESK+KD" if Kuntaid==171 & vaalivuosi==2008 & (Puolue=="KESK" | Puolue=="KD")
replace alliance="KOK+KD" if Kuntaid==249 & vaalivuosi==2008 & (Puolue=="KOK" | Puolue=="KD")
replace alliance="KESK+VIHR" if Kuntaid==300 & vaalivuosi==2008 & (Puolue=="KESK" | Puolue=="VIHR")
replace alliance="KOK+KD" if Kuntaid==400 & vaalivuosi==2008 & (Puolue=="KOK" | Puolue=="KD")
replace alliance="KESK+KD+RKP" if Kuntaid==858 & vaalivuosi==2008 & (Puolue=="KESK" | Puolue=="KD" | Puolue=="RKP")
replace alliance="STP+SKP" if Kuntaid==858 & vaalivuosi==2008 & (Puolue=="STP" | Puolue=="SKP")

* Keep years for which we have VAA data
keep if vaalivuosi==2012 | vaalivuosi==2008 

gen alliance2=alliance
replace alliance2=Puolue if alliance==""
egen listnumb=group(alliance2)

* check
sort vaalivuosi Kuntaid Puolue
order vaalivuosi Kuntaid Puolue alliance alliance2 listnumb

keep hetu Ehdokasnro Kuntaid vaalivuosi Puolue listnumb Ika university unemployed sukupuoli aanetyht valittu KunnanValtuusto highprofessional student ansiotulo valtuustokoko koko_vaesto
rename Ehdokasnro tunnus
rename Kuntaid munid
rename vaalivuosi year
rename Puolue party
rename listnumb party2
rename koko_vaesto population

sort munid tunnus year

* invid chars
gen male=(sukupuoli=="M")
gen old=(Ika>60)

* vote shares and seat shares
by year munid, sort: egen munvotes=sum(aanetyht) 
by year munid party2, sort: egen partyvotes=sum(aanetyht) 
gen partyvoteshare=partyvotes/munvotes 

by year munid, sort: egen munseats=sum(valittu) 
by year munid party2, sort: egen partyseats=sum(valittu) 
gen partyseathare=partyseats/munseats 

* nroofparties
cap drop tempparty
by year munid party2, sort: gen tempparty=1 if _n==1 
bysort year munid: egen nroparty=sum(tempparty)

cap drop tempmun
by year munid, sort: gen tempmun=1 if _n==1 
tab nroparty if tempmun==1 & year==2008

* Effective number of parties
gen partyvoteshare2=partyvoteshare*partyvoteshare
by year munid, sort: egen effnumbpartytemp=sum(partyvoteshare2) if tempparty==1
by year munid, sort: egen HIparty=mean(effnumbpartytemp) 
gen effnumbparty=1/HIparty
drop effnumbpartytemp partyvoteshare2 HIparty
tab effnumbparty if tempmun==1 & year==2008

* number of candidates per municipality
gen cm=1
bysort munid year: egen muncands=sum(cm)
drop cm

* number of candidates per alliance
gen b=1
bysort munid party2 year: egen listcands=sum(b)
* number of candidates per party label
bysort munid party year: egen partycands=sum(b)

* max list size
gen maxlist=1.5*valtuustokoko-0.5
gen diffmaxlist=maxlist-listcands
sum listcands maxlist diffmaxlist 

tab diffmaxlist if tempparty==1 & year==2008 & population<22500 
tab diffmaxlist if tempparty==1 & year==2008 & population>22500 

* merge YLE VAA data

merge 1:1 munid tunnus year using "Proportionality2.dta"

save "beforeindexing.dta", replace

drop if _merge!=3
drop _merge

* drop party lists with 5 or less respondent
gen a=1
bysort munid party year: egen suma=sum(a)
drop if suma<6

* temps again due to new merge and omitting data
cap drop tempparty
by year munid party2, sort: gen tempparty=1 if _n==1 
cap drop tempmun
by year munid, sort: gen tempmun=1 if _n==1 

* cohesion indices
local vars "health_priv daycare soc_welfare anti_environment progressive sell subsidy personnel  proptax_resident proptax_leisure fees plots aid merger newfees school_priv old_priv child_priv engin_priv soc_priv alko_priv fire_priv zone_priv spechealth_priv water_priv no_priv tax healthcare"

foreach x of varlist `vars' {
	bysort munid party2: egen mean`x'=mean(`x') if year==2008
	gen distance`x'=(`x'-mean`x')*(`x'-mean`x') if year==2008
}

gen distance_r=sqrt(distanceprogressive+distancesoc_welfare+distanceproptax_resident+distanceproptax_leisure+distancefees+distancenewfees) if year==2008
gen distance_all=sqrt(distancehealth_priv + distancedaycare + distancesoc_welfare + distanceanti_environment + distanceprogressive + distancesell + distancesubsidy + distancepersonnel + distanceproptax_resident + distanceproptax_leisure + distancefees + distanceplots + distanceaid + distancemerger + distancenewfees + distanceschool_priv + distanceold_priv + distancechild_priv + distanceengin_priv + distancesoc_priv + distancealko_priv + distancefire_priv + distancezone_priv + distancespechealth_priv + distancewater_priv + distanceno_priv + distancetax + distancehealthcare)

gen progressive2=progressive
gen sell2=sell
gen soc_welfare2=soc_welfare
gen daycare2=daycare
gen anti_environment2=anti_environment
gen subsidy2=subsidy

local vars "eldercare healthfees refugees daycare2 marginalization soc_welfare2 recycling roads anti_environment2 proptax referendum voting_age progressive2 no_MPs merger_notice no_municemploy debt2_ cut_old cut_child soc_more daycare_more old_more school_more nurse_more health_more spechealth_more cut_service more_fees more_tax sell2 subsidy2 debt health_priv2 trade_off2 hist_build2 diverse2"

foreach x of varlist `vars' {
	bysort munid party2: egen mean`x'2012=mean(`x') if year==2012
	gen distance`x'=(`x'-mean`x'2012)*(`x'-mean`x'2012) if year==2012
}
 
replace distance_r=sqrt(distancemore_fees+distancemore_tax+distanceprogressive2+distancedebt+distancedebt2+distancesoc_welfare2+distancehealthfees+distanceproptax) if year==2012
replace distance_all=sqrt(distanceeldercare + distancehealthfees + distancerefugees + distancedaycare2 + distancemarginalization + distancesoc_welfare2 + distancerecycling + distanceroads + distanceanti_environment2 + distanceproptax + distancereferendum + distancevoting_age + distanceprogressive2 + distanceno_MPs + distancemerger_notice + distanceno_municemploy + distancedebt2_ + distancecut_old + distancecut_child + distancesoc_more + distancedaycare_more + distanceold_more + distanceschool_more + distancenurse_more + distancehealth_more + distancespechealth_more + distancecut_service + distancemore_fees + distancemore_tax + distancesell2 + distancesubsidy2 + distancedebt + distancehealth_priv2 + distancetrade_off2 + distancehist_build2 + distancediverse2) if year==2012

* munipality and party level distance measures
bysort munid party2 year: egen meanpartydist_r=mean(distance_r) 
bysort munid party2 year: egen meanpartydist_all=mean(distance_all) 
bysort munid year: egen meanmundist_r=mean(distance_r) 
bysort munid year: egen meanmundist_all=mean(distance_all) 


* final data format
rename munid munic
rename tunnus Ehdokasnro

keep Ehdokasnro munic year valtuustokoko party party2 suma nroparty effnumbparty muncands listcands partycands old university unemployed male Ika KunnanValtuusto highprofessional student ansiotulo valittu tempmun tempparty partyseathare partyvoteshare distance_r distance_all meanpartydist_r meanpartydist_all meanmundist_r meanmundist_all population 

sort munic year Ehdokasnro

save "vaalikone for merging 17022024 alliances.dta", replace

merge m:m munic year using "munic_chars.dta"
* drop years 1996-2008
drop if _merge==2
*drops over 45k municipalities and those that did a municipal merger that affects 2008 elections
drop if _merge==1
drop _merge

drop if pop_el>22500

gen pop_el0=.
replace pop_el0=pop_el-2000 if pop_el>0 & pop_el<=3000
replace pop_el0=pop_el-4000 if pop_el>3000 & pop_el<=6000
replace pop_el0=pop_el-8000 if pop_el>6000 & pop_el<=11500
replace pop_el0=pop_el-15000 if pop_el>11500 & pop_el<=22500

gen year2008=(year==2008)

* scale outcome by number of questions per year
sum distance_all distance_r if year==2008	
sum distance_all distance_r if year==2012	
gen distance_all2=.
replace distance_all2=distance_all/28 if year==2008	
replace distance_all2=distance_all/36 if year==2012	
gen distance_r2=.
replace distance_r2=distance_r/6 if year==2008	
replace distance_r2=distance_r/8 if year==2012	
sum distance_all distance_r distance_all2 distance_r2 if year==2008	
sum distance_all distance_r distance_all2 distance_r2 if year==2012	

* munipality and party level distance measures
bysort munic party2 year: egen meanpartydist_r2=mean(distance_r2) 
bysort munic party2 year: egen meanpartydist_all2=mean(distance_all2) 
bysort munic year: egen meanmundist_r2=mean(distance_r2) 
bysort munic year: egen meanmundist_all2=mean(distance_all2) 

* scale outcome measures with number of respondents
gen number=1
bysort munic party2 year: egen nrorespondentsparty=sum(number) 
gen scaling=nrorespondentsparty/(nrorespondentsparty-1)

gen distance_all2s=distance_all2*scaling 
gen distance_r2s=distance_r2*scaling
bysort munic party2 year: egen meanpartydist_r2s=mean(distance_r2s) 
bysort munic party2 year: egen meanpartydist_all2s=mean(distance_all2s) 
bysort munic year: egen meanmundist_r2s=mean(distance_r2s) 
bysort munic year: egen meanmundist_all2s=mean(distance_all2s)

gen candsperseats=muncands/valtuustokoko
sum candsperseats if tempmun==1

cd "path"

save "vaalikoneRDD_17022024_alliances_analysis_s_temp.dta", replace

* Main data

use "vaalikoneRDD_17022024_alliances_analysis_s_temp.dta", clear

keep munic year valtuustokoko party party2 suma nroparty effnumbparty muncands listcands partycands candsperseats valittu tempmun tempparty partyseathare partyvoteshare distance_r2s distance_all2s meanpartydist_r2s meanpartydist_all2s meanmundist_r2s meanmundist_all2s pop_el pop_el0 veropc vosuuspc yli65vpc personnel expenditurepc tyottom

save "vaalikoneRDD_17022024_alliances_analysis_s.dta", replace

* Separate data for individual level characteristics for data security reasons

use "vaalikoneRDD_17022024_alliances_analysis_s_temp.dta", clear

keep munic year old university unemployed male Ika KunnanValtuusto valittu tempmun tempparty pop_el pop_el0 

set seed 12345
tempvar sortorder
gen `sortorder' = runiform()
sort `sortorder'

save "vaalikoneRDD_17022024_alliances_analysis_invidbalance.dta", replace

* reponse rates data

use "beforeindexing.dta", clear
keep munid party2 party year tempmun listcands muncands tempparty male Ika KunnanValtuusto aanetyht valittu university unemployed old _merge

set seed 54321
tempvar sortorder
gen `sortorder' = runiform()
sort `sortorder'

save "responserates.dta", replace

* Data for Table A4

use "beforeindexing.dta", clear
keep year munid party party2 tunnus hetu sukupuoli Ika KunnanValtuusto aanetyht valittu university unemployed student highprofessional valtuustokoko male old soc_welfare anti_environment progressive sell subsidy daycare 
sum 

* drop if did not respond
drop if soc_welfare==. 
drop if anti_environment==.  
drop if progressive==.  
drop if sell==.  
drop if subsidy==.  
drop if daycare==.  

* keep only re-running twice-responding candidates
gen tempvar=1
by hetu, sort: egen nrotimes=sum(tempvar) 
tab nrotimes
keep if nrotimes==2
drop nrotimes
sum

* Party swichers variable (=treatment)
gen fixedpartycode=9
replace fixedpartycode=1 if party=="KOK"
replace fixedpartycode=2 if party=="KESK"
replace fixedpartycode=3 if party=="SDP"
replace fixedpartycode=4 if party=="VAS"
replace fixedpartycode=5 if party=="VIHR"
replace fixedpartycode=6 if party=="KD"
replace fixedpartycode=7 if party=="PS"
replace fixedpartycode=8 if party=="RKP"
replace fixedpartycode=. if party==""

by hetu, sort: egen switchertemp=sd(fixedpartycode) 
gen switcher=(switchertemp>0)
* sum, 5.3% are switchers, 16110 obs, 8550 candidates in total that re-run and respond both years

sort munid hetu
local vars "daycare soc_welfare anti_environment progressive sell subsidy valtuustokoko"
foreach x of varlist `vars' {
	gen `x'2008temp=`x' if year==2008
	by hetu, sort: egen `x'2008=mean(`x'2008temp) 
	drop `x'2008temp
	gen `x'2012temp=`x' if year==2012
	by hetu, sort: egen `x'2012=mean(`x'2012temp)
	drop `x'2012temp
	gen change`x'= `x'2012 - `x'2008
	gen abschange`x'=abs(change`x')
}

keep abschangedaycare abschangesoc_welfare abschangeanti_environment abschangeprogressive abschangesell abschangesubsidy switcher abschangevaltuustoko munid fixedpartycode KunnanValtuusto university unemployed student highprofessional male old Ika year  

save "TableA4.dta", replace

* Data for Table A5

use "vaalikone for merging 17022024 alliances.dta", clear

gen year2008=(year==2008)

* scale outcome by number of questions per year
sum distance_all distance_r if year==2008	
sum distance_all distance_r if year==2012	
gen distance_all2=.
replace distance_all2=distance_all/28 if year==2008	
replace distance_all2=distance_all/36 if year==2012	
gen distance_r2=.
replace distance_r2=distance_r/6 if year==2008	
replace distance_r2=distance_r/8 if year==2012	
sum distance_all distance_r distance_all2 distance_r2 if year==2008	
sum distance_all distance_r distance_all2 distance_r2 if year==2012	

* munipality and party level distance measures
bysort munic party2 year: egen meanpartydist_r2=mean(distance_r2) 
bysort munic party2 year: egen meanpartydist_all2=mean(distance_all2) 
bysort munic year: egen meanmundist_r2=mean(distance_r2) 
bysort munic year: egen meanmundist_all2=mean(distance_all2) 

* scale outcome measures with number of respondents
gen number=1
bysort munic party2 year: egen nrorespondentsparty=sum(number) 
gen scaling=nrorespondentsparty/(nrorespondentsparty-1)

gen distance_all2s=distance_all2*scaling 
gen distance_r2s=distance_r2*scaling
bysort munic party2 year: egen meanpartydist_r2s=mean(distance_r2s) 
bysort munic party2 year: egen meanpartydist_all2s=mean(distance_all2s) 
bysort munic year: egen meanmundist_r2s=mean(distance_r2s) 
bysort munic year: egen meanmundist_all2s=mean(distance_all2s)

keep munic year distance_r2s distance_all2s meanmundist_r2s meanmundist_all2s population tempmun
save "TableA5.dta", replace





